package com.zh.api.controller.goods;


import com.alibaba.fastjson.JSONObject;
import com.zh.common.base.BaseController;
import com.zh.common.domain.UserDO;
import com.zh.common.utils.*;
import com.zh.goods.domain.GoodsDO;
import com.zh.goods.domain.GoodsOrderDO;
import com.zh.goods.domain.GoodsSublistDO;
import com.zh.goods.domain.SiteDO;
import com.zh.goods.service.GoodsOrderService;
import com.zh.goods.service.GoodsService;
import com.zh.goods.service.GoodsSiteService;
import com.zh.goods.service.GoodsSublistService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品订单管理
 * 
 * @author beicheng
 * @email 1992lcg@163.com
 * @date 2019-01-09 20:48:53
 */
 
@Controller
@RequestMapping("/goods/order")
@Api("商品订单管理")
public class GoodsOrderController extends BaseController {
	@Autowired
	private GoodsOrderService scGoodsOrderReceiptService;

	@Autowired
	private GoodsService goodsService;

	@Autowired
	private GoodsSublistService goodsSublistService;

	@Autowired
	private GoodsSiteService siteService;
	
	@ResponseBody
	@RequestMapping(value = "list", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation("商品订单列表")
	public PageUtils list(@RequestBody Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
        query.put("servicerId",getCompanyUser().getCompanyId());
		List<GoodsOrderDO> scGoodsOrderReceiptList = scGoodsOrderReceiptService.list(query);
		int total = scGoodsOrderReceiptService.count(query);
		PageUtils pageUtils = new PageUtils(scGoodsOrderReceiptList, total);
		return pageUtils;
	}


	@RequestMapping(value = "getOrder", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation("获取商品订单详情")
	ResultVO getOrder(@RequestBody GoodsOrderDO goodsOrderDO){
		GoodsOrderDO scGoodsOrderReceipt = scGoodsOrderReceiptService.get(goodsOrderDO.getId());
	    return ResultVO.success(scGoodsOrderReceipt);
	}
	

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping(value = "update", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation("修改商品订单")
	public ResultVO update(@RequestBody GoodsOrderDO scGoodsOrderReceipt){
		scGoodsOrderReceiptService.update(scGoodsOrderReceipt);
		return ResultVO.success();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value = "remove", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation("删除商品订单")
	public ResultVO remove(@RequestBody GoodsOrderDO scGoodsOrderReceipt){
		if(scGoodsOrderReceiptService.remove(scGoodsOrderReceipt.getId())>0){
		return ResultVO.success();
		}
		return ResultVO.error();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value = "batchRemove", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation("批量删除商品订单")
	public ResultVO batchRemove(@RequestBody JSONObject jsonParam){
		String[] ids= (String[]) jsonParam.get("ids");
		if(scGoodsOrderReceiptService.batchRemove(ids)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}

	@RequestMapping(value = "saveGoodsOrder", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation("商品下单")
	@ResponseBody
	public ResultVO saveGoodsOrder(@RequestBody Map<String, Object> params) {

		UserDO user = getUser();
		GoodsOrderDO scGoodsOrderReceiptDO=new GoodsOrderDO();

		if (CheckEmptyUtil.isEmpty(params)) {
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}

		String goodsId = null;
		String subgoodsId=null;
		String siteId=null;
		String remark=null;
		int num=0;
		try {
			goodsId = String.valueOf(params.get("goodsId"));
			subgoodsId = String.valueOf(params.get("subgoodsId"));
			siteId = String.valueOf(params.get("siteId"));
			remark=params.get("remark")+"";
			num = Integer.valueOf(params.get("num") + "");
		}catch (Exception e){
			logger.error("AppController saveGoodsOrder has error: " + e.getMessage());
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}

		if(CheckEmptyUtil.isEmpty(goodsId)
				||CheckEmptyUtil.isEmpty(siteId)
				||num==0){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}

		scGoodsOrderReceiptDO.setGoodsId(goodsId);
		scGoodsOrderReceiptDO.setGoodsSubId(subgoodsId);
		scGoodsOrderReceiptDO.setGoodsNum(num);

		GoodsDO scGoodsDO = goodsService.get(goodsId);
		if (scGoodsDO == null) {
			return ResultVO.error(ResultEnum.NO_GOODS_ERROR);
		}

		if (scGoodsDO.getExamineStatus()!=2) {
			return ResultVO.error(ResultEnum.NO_GOODS_ERROR);
		}

		if (scGoodsDO.getFlag() == 0) {
			return ResultVO.error(ResultEnum.GOODS_DOWN_ERROR);
		}


		scGoodsOrderReceiptDO.setServicerId(scGoodsDO.getServicerId());
		scGoodsOrderReceiptDO.setServicerName(scGoodsDO.getServicerName());
		scGoodsOrderReceiptDO.setGoodsImg(scGoodsDO.getGoodsImg());

		if(CheckEmptyUtil.isEmpty(subgoodsId)){
			scGoodsOrderReceiptDO.setGoodsName(scGoodsDO.getGoodsName());
			scGoodsOrderReceiptDO.setGoodsMoney(scGoodsDO.getGoodsCurrentPrice());
		}else {
			GoodsSublistDO scGoodsSublistDO = goodsSublistService.get(subgoodsId);
			if (scGoodsSublistDO == null) {
				return ResultVO.error(ResultEnum.NO_GOODS_ERROR);
			}

			if (scGoodsSublistDO.getFlag() == 0) {
				return ResultVO.error(ResultEnum.GOODS_DOWN_ERROR);
			}

			scGoodsOrderReceiptDO.setGoodsMoney(scGoodsSublistDO.getGoodsCurrentPrice());
			scGoodsOrderReceiptDO.setGoodsName(scGoodsSublistDO.getGoodsName());
			scGoodsOrderReceiptDO.setSpecification(scGoodsSublistDO.getSpecification());
			scGoodsOrderReceiptDO.setColour(scGoodsSublistDO.getColour());
		}


		SiteDO siteDO = siteService.get(siteId);
		if(siteDO==null){
			return ResultVO.error(ResultEnum.NO_SITE_ERROR);
		}


		scGoodsOrderReceiptDO.setArea(siteDO.getArea());
		scGoodsOrderReceiptDO.setAdress(siteDO.getAdress());
		scGoodsOrderReceiptDO.setRemark(remark);
		scGoodsOrderReceiptDO.setReceiveName(siteDO.getName());
		scGoodsOrderReceiptDO.setReceivePhone(siteDO.getUserPhone());
		scGoodsOrderReceiptDO.setSiteId(siteDO.getId());

		if(scGoodsOrderReceiptService.saveOrderAndOrderStatus(scGoodsOrderReceiptDO)>0){
			Map<String,Object> map = new HashMap();
			map.put("orderNo",scGoodsOrderReceiptDO.getId());
			return ResultVO.success(map);
		}

		return ResultVO.error();
	}
	
}
